package com.bling.service;

import java.io.Serializable;
import java.sql.SQLException;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.bling.common.bean.Kind;
import com.bling.common.idao.IKindDao;
import com.bling.common.page.PageResult;
import com.bling.iservice.IKindService;
@Service
public class KindService implements IKindService{

	public void deleteKind(Kind kind) throws SQLException {
		kindDao.delete(kind);
	}
	public void deleteKindById(Serializable id) throws SQLException {
		kindDao.delete(Kind.class, id);
	}
	public Kind findKindById(Serializable id) throws SQLException {
		return kindDao.find(Kind.class, id);
	}
	public PageResult<Kind> listKindAll() throws SQLException {
		String hql = "from Kind k order by k.sort desc";
		return kindDao.listAll(hql);
	}
	public PageResult<Kind> listKindByPage(int from, int num) throws SQLException {
		String hql = "from Kind k order by k.sort desc";
		return kindDao.pageList(hql, from, num);
	}
	public void saveKind(Kind kind) throws SQLException {
		kindDao.save(kind);
	}
	public void updateKind(Kind kind) throws SQLException {
		kindDao.update(kind);
	}
	public void updateKindSort(Serializable id, Integer sort)
			throws SQLException {
		String hql = "update Kind k set k.sort=? where k.id=?";
		kindDao.update(hql,new Object[]{sort,id});
	}
	
	public PageResult<Kind> listKindPageByPid(Serializable pid,int from,int num) throws SQLException{
		String hql = "from Kind k where k.parentid=?";
		return kindDao.pageList(hql,new Object[]{pid}, from, num);
	}
	
	public PageResult<Kind> listKindAllByPid(Serializable pid) throws SQLException{
		String hql = "from Kind k where k.parentid=?";
		return kindDao.listAll(hql, new Object[]{pid});
	}
	
	public PageResult<Kind> searchKind(String keyword,int from,int num) throws SQLException{
		String hql = "from Kind k where k.name like '%"+keyword+"%' order by k.sort desc";
		return kindDao.pageList(hql, from, num);		
	}
	
	@Resource
	private IKindDao kindDao;
	public IKindDao getKindDao() {
		return kindDao;
	}
	public void setKindDao(IKindDao kindDao) {
		this.kindDao = kindDao;
	}
}
